import Taro, {Component, Config} from '@tarojs/taro'
import { Button, View } from '@tarojs/components'
import './index.scss'
import { toHomePage } from '@/utils/page_router'
import { clearUserStorage, logout } from '@/utils/storage'
import { asyncWechatInfo } from "@/api/userApi"

export default class Index extends Component {
  state: any
  config: Config = {
    navigationBarTitleText: '设置'
  }

  //  同步用户信息
  syncUserInfo (res) {
    const { userInfo: { nickName, avatarUrl } } = res.detail
    wx.showLoading({title: '同步中...', mask: true})
    asyncWechatInfo({ nickName, headImg: avatarUrl }).then(() => {
      wx.hideLoading()
      wx.showToast({icon: 'success', title: '同步成功'})
    }).catch(() => {
      wx.hideLoading()
      wx.showToast({icon: 'none', title: '同步失败'})
    })
  }

  //  清除缓存
  clearStorage () {
    Taro.showLoading({title: '清除中...', mask: true})
    try {
      clearUserStorage()
      // const fileSystemManager = wx.getFileSystemManager()
      // const fileList = fileSystemManager.readdirSync(wx.env.USER_DATA_PATH)
      //
      // fileList.map(url => {
      //   removeFile(`${wx.env.USER_DATA_PATH}/${url}`).then()
      // })
      Taro.showToast({icon: 'none', title: '清除成功'})
      setTimeout(() => {
        Taro.hideLoading()
        // Taro.navigateBack()
        window.location.href = '/'
      }, 500)
    } catch (e) {
      Taro.showToast({icon: 'none', title: '清除失败'})
    }
  }

  //  退出登录
  onLogout() {
    Taro.showLoading({ title: '请稍后...', mask: true })
    logout()

    Taro.showToast({
      mask: true,
      title: '退出成功',
      success: function () {
        setTimeout(() => {
          Taro.hideLoading()
          window.location.href = '/'
          // toHomePage()
        }, 800)
      }
    })
  }

  //  打开微信地址
  openWechatAddress() {
    wx.chooseAddress()
  }

  //  打开微信设置
  openWechatSetting() {
    wx.openSetting()
  }

  //  tab-bar 适配
  onTabBarAdapter() {
    wx.showLoading({ title: '请稍后...', mask: true })
    setTimeout(() => {
      const logger = wx.getRealtimeLogManager()
      logger.info('tabBar适配：', JSON.stringify(wx.getSystemInfoSync()))

      wx.hideLoading()
      wx.showToast({ title: '提交成功' })
    }, 1500)
  }

  render() {
    return (
      <View className='setting-page'>
        <View className='page-scroll'>
          <View className='setting-list'>
            <Button className='list-item thin-border__b' onClick={this.openWechatAddress.bind(this)}>
              <View className='item-title'>微信地址</View>
              <View className='item-icon bnn-icon'>&#xe866;</View>
            </Button>
            <Button className='list-item' onClick={this.openWechatSetting.bind(this)}>
              <View className='item-title'>微信授权</View>
              <View className='item-icon bnn-icon'>&#xe866;</View>
            </Button>
          </View>
          <View className='setting-list'>
            <Button className='list-item thin-border__b' openType='getUserInfo' onGetUserInfo={this.syncUserInfo.bind(this)}>
              <View className='item-title'>同步用户基本信息</View>
              <View className='item-icon bnn-icon'>&#xe866;</View>
            </Button>
            <Button className='list-item thin-border__b' onClick={this.onLogout.bind(this)}>
              <View className='item-title'>退出登录</View>
              <View className='item-icon bnn-icon'>&#xe866;</View>
            </Button>
            <Button className='list-item thin-border__b' onClick={this.clearStorage.bind(this)}>
              <View className='item-title'>清除缓存</View>
              <View className='item-icon bnn-icon'>&#xe866;</View>
            </Button>
          </View>
        </View>
      </View>
    )
  }
}
